<?php
/**
 * Created by PhpStorm.
 * User: Da Xiong
 * Date: 2018/11/28
 * Time: 17:34
 */
namespace App\Models;


use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class VideosCount extends Model
{
    protected $table = 'videos_count';
    protected $guarded = [];
    public $timestamps = false;
    protected $dates = ['created_at'];

    public function video(){
        return $this->belongsTo('App\Models\Videos', 'video_id', 'id');
    }

    /**
     * 更新统计
     * @param int $uid
     * @param string $type
     * @return mixed
     */
    public function updateCount($videoData = [],$type = 'views'){
        $today = (string)date('Y-m-d',time());
        $findVideoCount = $this->where('video_id',$videoData['id'])->where('date',$today)->first();
        if($findVideoCount && $type){
            $findVideoCount->increment($type);
        }else{
            $data['video_id'] = $videoData['id'];
            $data['admin_id'] = $videoData['userid'];
            $data['date'] = $today;
            $data['created_at'] = Carbon::now();
            $data[$type] = 1;
            $this->create($data);
        }
    }
}